//
//  YWTabBar.m
//  maintain
//
//  Created by 郑想 on 15/10/9.
//  Copyright (c) 2015年 ewin. All rights reserved.
//

#import "YWTabBar.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "UIView+Extension.h"
#import "UIButton+Animations.h"

@interface YWTabBar ()
@property (nonatomic, weak) UIButton* scanBtn;
@end

@implementation YWTabBar

+ (void)initialize
{
    [UITabBar appearance].tintColor = [UIColor customRedColor];
    
    [UITabBar appearance].itemPositioning = UITabBarItemPositioningCentered;
    [UITabBar appearance].itemWidth = SCREEN_WIDTH / 3;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIButton* scanBtn = [[UIButton alloc] init];
        scanBtn.backgroundColor = [UIColor clearColor];
        [scanBtn setImage:[UIImage imageNamed:@"tabbar_scan"] forState:UIControlStateNormal];
        [scanBtn setImage:[UIImage imageNamed:@"tabbar_scan_highlighted"] forState:UIControlStateHighlighted];
        [scanBtn setTitle:@"扫一扫" forState:UIControlStateNormal];
        [scanBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [scanBtn setTitleColor:[UIColor customRedColor] forState:UIControlStateHighlighted];
        scanBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        scanBtn.size = CGSizeMake(SCREEN_WIDTH / 3-10, 49);
        scanBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        scanBtn.imageEdgeInsets = UIEdgeInsetsMake(3, SCREEN_WIDTH / 6 - 30, 19 - 5, -10);
        scanBtn.titleEdgeInsets = UIEdgeInsetsMake(35, -40, 0, -SCREEN_WIDTH / 6 + 45);

        [scanBtn addTarget:self action:@selector(scanClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:scanBtn];
        _scanBtn = scanBtn;
    }
    return self;
}

- (void)scanClick:(UIButton*)sender
{
    // [sender shakeWithVelocity:@300
    //         completionBlock:^{
    if ([self.tabBardelegate respondsToSelector:@selector(tabBarDidClickScanButton:)]) {
        [self.tabBardelegate tabBarDidClickScanButton:self];
    }
    //       }];
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.scanBtn.centerX = self.width * 0.5;
    self.scanBtn.centerY = self.height * 0.5;

    NSInteger tabbarButtonW = self.width / 3;

    CGFloat tabbarButtonIndex = 0;
    for (UIView* child in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        if ([child isKindOfClass:class]) {
            child.width = tabbarButtonW;
            child.x = tabbarButtonIndex * tabbarButtonW;
            tabbarButtonIndex++;
            if (tabbarButtonIndex == 1) {
                tabbarButtonIndex++;
            }
        }
    }
}
@end
